package com.solcoo.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;

public class ImageUtil implements Runnable {

	private static final Logger logger = Logger.getLogger(ImageUtil.class);
	private String picPath;
	private String picName;
	private int width;
	
	public ImageUtil(String picPath, String picName, int width) {
		super();
		this.picPath = picPath;
		this.picName = picName;
		this.width = width;
	}

	public ImageUtil() {
		super();
	}

	/**
	 * 压缩图片
	 * @param picPath
	 * @param picName
	 * @param width
	 * @return
	 */
	public static String zoomPicture(String picPath, String picName, int width) {
		String filePath = picPath + "/" + width + "/" + picName;
		
		File file = new File(filePath);
		
		BufferedImage srcBufImage = null;
		int newHeight = 0;
		
		if ( !file.exists() ) {
			File folder = new File(picPath + "/" + width);
			
			if (!folder.exists())
				folder.mkdirs();
			
			BufferedImage targetBufImage = null;
			
			try {
				srcBufImage = ImageIO.read(new File(picPath + "/" + picName));
				
				double rate = width * 1.0 / srcBufImage.getWidth();
				
				newHeight = (int) (srcBufImage.getHeight() * rate);
				
				targetBufImage = new BufferedImage(width, newHeight, BufferedImage.TYPE_INT_RGB);
				
				Image image = srcBufImage.getScaledInstance(width, newHeight, Image.SCALE_SMOOTH);
				
				targetBufImage.getGraphics().drawImage(image, 0, 0, null);
				
				file.createNewFile();
				
				ImageIO.write(targetBufImage, "jpg", file);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		} else {
			try {
				srcBufImage = ImageIO.read(new File(filePath));
				
				newHeight = srcBufImage.getHeight();
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
		
		return newHeight + "";
	}
	
	/**
	 * 截取图片
	 * @param srcPath
	 */
	public static void cutPicture(String picPath, String picName) {
		String filePath = picPath + "/square/" + picName;
		File file = new File(filePath);
		
		BufferedImage srcBufImage = null;
		BufferedImage targetBufImage = null;
		
		int sideLength = 0; // 边长
		int x = 0;
		int y = 0;
		
		if ( !file.exists() ) {
			File folder = new File(picPath + "/square");
			
			if (!folder.exists())
				folder.mkdirs();
			
			try {
				srcBufImage = ImageIO.read(new File(picPath + "/" + picName));
				
				int width = srcBufImage.getWidth();
				int height = srcBufImage.getHeight();
				
				if (width < height) {
					sideLength = width;
					x = 0;
					y = height/2 - width/2;
				} else {
					sideLength = height;
					x = width/2 - height/2;
					y = 0;
				}
				
				targetBufImage = new BufferedImage(sideLength, sideLength, BufferedImage.TYPE_INT_RGB);
				Image image = srcBufImage.getSubimage(x, y, sideLength, sideLength);
				
				targetBufImage.getGraphics().drawImage(image, 0, 0, null);

				file.createNewFile();
					
				ImageIO.write(targetBufImage, "jpg", file);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
	}

	@Override
	public void run() {
		zoomPicture(picPath, picName, width);
	}
}